<template>
  <div class="detail-container">
    <el-form
      ref="postForm"
      :model="postForm"
      :rules="rules"
      label-width="100px"
      label-suffix=":"
    >

      <sticky :z-index="10" :class-name="'sub-navbar ' + postForm.color">
        <el-button v-loading="loading" type="success" @click="submitForm">
          保存
        </el-button>
      </sticky>

      <div class="app-container" style="padding: 10px 20px">
        <el-row>
          <el-col :span="22">
            <el-form-item label="下单ID" required prop="id">
              <el-input v-model.trim="postForm.id" style="width: 200px" />
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model.trim="postForm.remark" style="width: 400px" />
            </el-form-item>
            <el-form-item label="商品" required prop="product">
              <el-select
                v-model="postForm.product"
                placeholder="商品"
                class="filter-item"
                filterable
                clearable
                style="width: 400px"
              >
                <el-option
                  v-for="item in productList"
                  :key="item.id"
                  :label="item.title"
                  :value="item.id"
                />
              </el-select>
              <span style="margin-left: 10px;">可以搜索商品名称操作</span>
            </el-form-item>
            <el-form-item label="数量" prop="quantity" required>
              <el-input v-model.trim="postForm.quantity" style="width: 400px" />
            </el-form-item>
          </el-col>
        </el-row>
      </div>

    </el-form>
  </div>
</template>

<script>
import api from '@/api'
import Sticky from '@/components/Sticky'
export default {
  name: 'MemberBao',
  components: {
    Sticky,
  },
  data() {
    return {
      id: -1,
      loading: false,
      postForm: {
        color: 'draft',
        id: null,
        remark: '代客下单',
        product: null,
        quantity: 1
      },
      rules: {
      },
      productList: []
    }
  },
  created() {
    this.getProductList()
  },
  methods: {
    async getProductList() {
      const response = await api.product.all()
      this.productList = response.data.list
    },
    submitForm() {
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          api.product.bao({
            id: this.id,
            ...this.postForm,
          }).then(response => {
            this.loading = false
            this.$notify({
              title: '成功',
              message: response.message || '操作成功',
              type: 'success',
              duration: 2000
            })
            this.$router.push('/order/list')
          }).finally(() => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.detail-container {
  //
}
</style>
